package com.smartwasser.yunhtgl.apphtgl.asps;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.model.TakePhotoOptions;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.function.OfficeReader;
import com.smartwasser.yunhtgl.apphtgl.photo.ImagePagerActivity;
import com.smartwasser.yunhtgl.apphtgl.photo.MainActivity1;
import com.smartwasser.yunhtgl.apphtgl.photo.PhotoAdapter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPGreat6Presenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPGreat6PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.utils.DialogUI;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.view.SPGreat6View;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.util.Log.d;
import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.dou361.dialogui.DialogUIUtils.showAlertHorizontal;
import static com.dou361.dialogui.DialogUIUtils.showBottomSheetAndCancel;
import static com.smartwasser.yunhtgl.apphtgl.base.BaseConstant.successSPGreat1SH;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.isBCVisible;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getGreat;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getPath;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getProcurementMode;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getQicaoType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getSZType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getTime;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getType;

/**
 * 上传合同图片接口
 * 个人未处理
 * 重大合同 上传电子盖章版合同
 */
public class SPGreat6Activity extends BaseActivity implements SPGreat6View {
    private SPGreat6Presenter sp6GreatPresenter = new SPGreat6PresenterImpl(this);
    private SPGreat1Bean type1Sp1GreatBean;
    private FlMessageBean mflMessageBean;
    private Dialog loadingHorizontal;
    private String fl_id;
    private String id;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.sp1_great_scroll)
    ScrollView sp1_great_scroll;
    /**
     * c_n_m_b
     */
    @BindView(R.id.sp1_great_code)
    TextView sp1_great_code;//合同编号
    @BindView(R.id.sp1_great_procurementMode)
    TextView sp1_great_procurementMode;//集采方式
    @BindView(R.id.sp1_great_tzs)
    TextView sp1_great_tzs;//中标通知书
    @BindView(R.id.sp1_great_lxd)
    TextView sp1_great_lxd;//工作联系单
    @BindView(R.id.sp1_great_qt)
    TextView sp1_great_qt;//其他资料
    @BindView(R.id.sp1_great_xz)
    TextView sp1_great_xz;//合同性质
    @BindView(R.id.sp1_great_name)
    TextView sp1_great_name;//合同名称
    @BindView(R.id.sp1_great_jia)
    TextView sp1_great_jia;//合同甲方
    @BindView(R.id.sp1_great_yi)
    TextView sp1_great_yi;//合同乙方
    @BindView(R.id.sp1_great_qtf)
    TextView sp1_great_qtf;//合同其他方
    @BindView(R.id.sp1_great_main)
    TextView sp1_great_main;//合同主要内容
    @BindView(R.id.sp1_great_bd)
    TextView sp1_great_bd;//合同标的
    @BindView(R.id.sp1_great_price)
    TextView sp1_great_price;//合同金额
    @BindView(R.id.sp1_great_start_time)
    TextView sp1_great_start_time;//合同执行日期
    @BindView(R.id.sp1_great_fen)
    TextView sp1_great_fen;//合同份数
    @BindView(R.id.sp1_great_sq)
    TextView sp1_great_sq;//合同申请部室
    @BindView(R.id.sp1_great_zgy)
    TextView sp1_great_zgy;//合同专管员
    @BindView(R.id.sp1_great_creat_time)
    TextView sp1_great_creat_time;//合同申请时间
    @BindView(R.id.sp1_great_fzr)
    TextView sp1_great_fzr;//部室负责人
    @BindView(R.id.sp1_great_yj)
    TextView sp1_great_yj;//法律意见书
    @BindView(R.id.sp1_great_sz)
    TextView sp1_great_sz;//收支情况
    @BindView(R.id.sp1_great_lx)
    TextView sp1_great_lx;//合同类型
    @BindView(R.id.sp1_great_qz)
    TextView sp1_great_qz;//签字盖章
    @BindView(R.id.sp1_great_qc)
    TextView sp1_great_qc;//起草
    @BindView(R.id.sp1_great_zw)
    TextView sp1_great_zw;//正文
    @BindView(R.id.sp1_great_bz)
    TextView sp1_great_bz;//备注
    @BindView(R.id.sp1_great_fl_all)
    LinearLayout sp1_great_fl_all;//补充信息可不可见
    @BindView(R.id.great_item_yy_time)
    TextView great_item_yy_time;//运营
    @BindView(R.id.great_item_yy_main)
    TextView great_item_yy_main;
    @BindView(R.id.great_item_yy_qz)
    TextView great_item_yy_qz;
    @BindView(R.id.great_item_fw_time)
    TextView great_item_fw_time;//法务
    @BindView(R.id.great_item_fw_main)
    TextView great_item_fw_main;
    @BindView(R.id.great_item_fw_qz)
    TextView great_item_fw_qz;
    @BindView(R.id.great_item_fg_time)
    TextView great_item_fg_time;//分管
    @BindView(R.id.great_item_fg_main)
    TextView great_item_fg_main;
    @BindView(R.id.great_item_fg_qz)
    TextView great_item_fg_qz;
    @BindView(R.id.great_item_zjl_time)
    TextView great_item_zjl_time;//分管
    @BindView(R.id.great_item_zjl_main)
    TextView great_item_zjl_main;
    @BindView(R.id.great_item_zjl_qz)
    TextView great_item_zjl_qz;
    private List<TImage> updateMedia = new ArrayList<>();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sp_great6);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("提交归档");
        sp1_great_scroll.setVisibility(View.INVISIBLE);
        id = getIntent().getStringExtra("id");
    }

    @Override
    public void initData() {
        loadingHorizontal = showLoadingHorizontal(this, "加载中...").show();
        try {
            sp6GreatPresenter.getType1Sp1Great(getStringData(this, "JSESSIONID", ""), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, R.id.sp1_great_tzs_look, R.id.sp1_great_lxd_look, R.id.sp1_great_fl_chose, R.id.sp1_great_zw_all, R.id.linear_select_photo,R.id.sp1_great_sh})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sp1_great_tzs_look:
                if (null != type1Sp1GreatBean.getBuContractBaseInfo()
                        && type1Sp1GreatBean.getBuContractBaseInfo().getTongzhishuList().size() > 0) {
                    String path = type1Sp1GreatBean.getBuContractBaseInfo().getTongzhishuList().get(0).getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
            case R.id.sp1_great_lxd_look:
                if (null != type1Sp1GreatBean.getBuContractBaseInfo()
                        && type1Sp1GreatBean.getBuContractBaseInfo().getLianxidanList().size() > 0) {
                    String path = type1Sp1GreatBean.getBuContractBaseInfo().getLianxidanList().get(0).getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
            case R.id.sp1_great_zw_all:
                if (null != type1Sp1GreatBean.getBuContractBaseInfo()) {
                    String path = type1Sp1GreatBean.getBuContractBaseInfo().getPath();
                    Logger.e("path" + path);
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
            case R.id.sp1_great_fl_chose:
                if (null != mflMessageBean && mflMessageBean.getFlyjsList().size() > 0) {
                    commitShow(mflMessageBean);
                } else {
                    loadingHorizontal = showLoadingHorizontal(this, "请稍等...").show();
                    sp6GreatPresenter.getFlMessage(getStringData(this, "JSESSIONID", ""));
                }
                break;
            case R.id.sp1_great_sh:
                if (updateMedia.size() > 0) {
                    Logger.e("上传签字盖章版合同");
                    commitPhoto();
                } else {
                    showResult("请选择上传的签字盖章版合同");
                }
                break;
            case R.id.linear_select_photo:
                //跳转选择图片页面
                Intent intent = new Intent(SPGreat6Activity.this, MainActivity1.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("photos", (Serializable) updateMedia);
                intent.putExtras(bundle);
                startActivityForResult(intent, 0001);
                break;
        }
    }

    /**
     * 获取合同详情
     *
     * @param tb
     */
    @Override
    public void success(SPGreat1Bean tb) {

        if (null != tb && null != tb.getBuContractBaseInfo()) {
            this.type1Sp1GreatBean = tb;
            sp1_great_code.setText(tb.getBuContractBaseInfo().getCode());
            sp1_great_xz.setText(getGreat(tb.getBuContractBaseInfo().getContractNature()));
            sp1_great_lx.setText(getType(tb.getBuContractBaseInfo().getType()));
            sp1_great_qc.setText(getQicaoType(tb.getBuContractBaseInfo().getQicaoType()));
            sp1_great_zw.setText(getPath(tb.getBuContractBaseInfo().getPath()));
            if (tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getUnitPriceOpinion().length() > 0) {
                String money = tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getUnitPriceOpinion() + "\n" +
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMoney() + "元";
                sp1_great_price.setText(money);
            } else {
                String money = tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMoney() + "元";
                sp1_great_price.setText(money);
            }
            sp1_great_name.setText(tb.getBuContractBaseInfo().getName());
            sp1_great_procurementMode.setText(getProcurementMode(tb.getBuContractBaseInfo().getProcurementMode()));
            sp1_great_sz.setText(getSZType(tb.getBuContractBaseInfo().getShouzhiType()));
            if (null != tb.getBuContractBaseInfo().getTongzhishuList() && tb.getBuContractBaseInfo().getTongzhishuList().size() > 0) {
                sp1_great_tzs.setText(tb.getBuContractBaseInfo().getTongzhishuList().get(0).getName());
            }
            if (null != tb.getBuContractBaseInfo().getLianxidanList() && tb.getBuContractBaseInfo().getLianxidanList().size() > 0) {
                sp1_great_lxd.setText(tb.getBuContractBaseInfo().getLianxidanList().get(0).getName());
            }

            if (null != tb.getBuContractBaseInfo().getBuMajorContractSignInfoList() && tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().size() > 0) {
                sp1_great_bz.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getRemarks());
                sp1_great_jia.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getJiaFangName());
                sp1_great_yi.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getYiFangName());
                sp1_great_qtf.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getOtherFangName());
                sp1_great_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMainContent());
                sp1_great_bd.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractBiaodi());
                String time = getTime(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeType(),
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeStart(),
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeEnd(),
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTime());
                sp1_great_start_time.setText(time);
                sp1_great_fen.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractCount() + "份");
                sp1_great_sq.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDept().getName());
                sp1_great_zgy.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_great_creat_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDate());
                sp1_great_fzr.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractDwOfficer().getName());

                great_item_yy_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveCheckDate());
                great_item_yy_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveUpdateOpinion());
                great_item_yy_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveName().getName());
                great_item_fw_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptCheckDate());
                great_item_fw_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptUpdateOpinion());
                great_item_fw_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptOfficerName().getName());
                great_item_fg_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeUpdateDate());
                great_item_fg_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeOpinion());
                great_item_fg_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeLeader().getName());
                great_item_zjl_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerUpdateDate());
                great_item_zjl_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerOpinion());
                great_item_zjl_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerName().getName());
            }
            if (isBCVisible(tb.getBuContractBaseInfo().getAct().getTaskDefKey())) {
                sp1_great_fl_all.setVisibility(View.VISIBLE);
            } else {
                sp1_great_fl_all.setVisibility(View.GONE);
            }
        }
        sp1_great_scroll.setVisibility(View.VISIBLE);
        dismiss(loadingHorizontal);
    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
    }

    @Override
    public void successGetFlMessage(FlMessageBean flMessageBean) {
        this.mflMessageBean = flMessageBean;
        dismiss(loadingHorizontal);
        commitShow(flMessageBean);
    }

    @Override
    public void successCommitPhoto(BaseBean baseBean) {
        dismiss(loadingHorizontal);
        showResult("上传成功");
        setResult(successSPGreat1SH);
        finish();
    }

    public void commitShow(final FlMessageBean flMessageBean) {
        final List<String> strings = new ArrayList<>();
        for (int i = 0; i < flMessageBean.getFlyjsList().size(); i++) {
            strings.add(flMessageBean.getFlyjsList().get(i).getName());
        }
        showBottomSheetAndCancel(this, strings, "取消", new DialogUIItemListener() {
            @Override
            public void onItemClick(CharSequence text, int position) {
                sp1_great_yj.setText(flMessageBean.getFlyjsList().get(position).getName());
                fl_id = flMessageBean.getFlyjsList().get(position).getId();
            }
        }).show();
    }

    /**
     * 上传图片
     */
    public void commitPhoto() {
        final String procInsId = type1Sp1GreatBean.getBuContractBaseInfo().getAct().getProcInsId();
        showAlertHorizontal(this, "上传提示", "确定要上传签字盖章版合同吗？", new DialogUI() {
            @Override
            public void onPositive() {
                try {
                        sp6GreatPresenter.getCommitPhoto(getStringData(SPGreat6Activity.this, "JSESSIONID", ""), id,procInsId,updateMedia);
                } catch (Exception e) {
                    Logger.e("请求网络大法启动失败");
                }
            }
        }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0001:
                Bundle bundle = new Bundle();
                bundle = data.getExtras();
                updateMedia = (List<TImage>) bundle.getSerializable("photos");
                sp1_great_qz.setText("已选中" + updateMedia.size() + "张");
        }
    }
}